var $ = require('../../common/helper.js');
Page({
	data: {
		province_id: '',
		city_id: '',
		city_name: '请选择城市',
		casy: [],
		casy_disabled: true,
		row: {
			firm: '0.00',
			person: '0.00'
		},
		casy_id: '',
		money: '填写基数',
		casy_name: '请选择险种类型',
		placeholder: 'placeholder',
		btn: '',
		btn_x: '',
		gjj_btn: '',
		gjj_money: '',
		gjj_money_min: 0,
		gjj_money_max: 0
	},
	onLoad: function (options) {
		$.overload(false);
	},
	change: function (e) {
		var index = Number(e.detail.value);
		this.setData({
			casy_id: this.data.casy[index].id,
			money: this.data.casy[index].money,
			casy_name: this.data.casy[index].name
		});
	},
	toggleGjj: function () {
		var x = this.data.gjj_btn == 'btn-x';
		this.setData({
			gjj_money: x ? '' : this.data.gjj_money_min,
			gjj_btn: x ? '' : 'btn-x'
		});
	},
	changeGjj: function (e) {
		var money = Number(e.detail.value), btn = '';
		if (money == Number(this.data.gjj_money_min)) btn = 'btn-x';
		this.setData({
			gjj_money: e.detail.value,
			gjj_btn: btn
		});
	},
	setParam: function (type, data) {
		this.setData({
			province_id: data.province_id,
			city_id: data.city_id,
			city_name: data.name,
			placeholder: ''
		});
		var _this = this;
		$.get('api.php?app=cart&act=casy&city_id=' + data.city_id, function (json) {
			_this.setData({
				casy: json.data.casy,
				casy_disabled: !$.isArray(json.data.casy) || !json.data.casy.length,
				row: json.data.row,
				casy_id: json.data.casy[0].id,
				money: json.data.casy[0].money,
				casy_name: json.data.casy[0].name,
				btn: 'btn',
				btn_x: 'btn-x',
				gjj_btn: 'btn-x',
				gjj_money: json.data.row.money,
				gjj_money_min: json.data.row.money,
				gjj_money_max: json.data.row.max_money
			});
		});
	},
	reset: function () {
		this.setData({
			city_name: '请选择城市',
			casy: [],
			row: {
				firm: '0.00',
				person: '0.00'
			},
			casy_id: '',
			money: '填写基数',
			casy_name: '请选择险种类型',
			placeholder: 'placeholder',
			btn: '',
			btn_x: '',
			gjj_btn: '',
			gjj_money: '',
			gjj_money_min: 0,
			gjj_money_max: 0
		});
	},
	submit: function () {
		if (!this.data.province_id || !this.data.city_id || !this.data.casy_id) {
			return;
		}
		var money = this.data.gjj_money;
		if (money.length && (Number(money) < this.data.gjj_money_min || Number(money) > this.data.gjj_money_max)) {
			$.overloadError('公积金范围值为' + parseInt(this.data.gjj_money_min) + '~' + this.data.gjj_money_max);
			return;
		}
		wx.navigateTo({
			url: '/pages/home/casy_detail?province_id=' + this.data.province_id + '&city_id=' + this.data.city_id + '&casy_id=' + this.data.casy_id + '&gjj=' + this.data.gjj_money,
		});
	}
})